﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FPSWatcher : MonoBehaviour
{
    public static float f_Fps;
    public float f_UpdateInterval = 0.5f; //每个0.5秒刷新一次  
    private float f_LastInterval; //游戏时间  
    private int i_Frames = 0;//帧数  

    public bool SetTargetFrameRate = false;

    public int targetFrameRate = 60;



    void Awake()
    {
        if (SetTargetFrameRate)
        {
            Application.targetFrameRate = this.targetFrameRate;
        }
    }

    
    
#if DEBUG

    void OnGUI()
    {

        GUIStyle fontStyle = new GUIStyle();
        fontStyle.normal.background = null;    //设置背景填充

        if (f_Fps > 50)
        {
            fontStyle.normal.textColor = new Color(0, 1, 0);
        }
        else if (f_Fps > 40)
        {
            fontStyle.normal.textColor = new Color(1, 1, 0);
        }
        else
        {
            fontStyle.normal.textColor = new Color(1.0f, 0, 0);
        }

        fontStyle.fontSize = 40;


        GUI.Label(new Rect(10, 10, 100, 30), "FPS:" + f_Fps.ToString("f2"), fontStyle);


    }
    
    void Update()
    {
        ++i_Frames;

        if (Time.realtimeSinceStartup > f_LastInterval + f_UpdateInterval)
        {
            f_Fps = i_Frames / (Time.realtimeSinceStartup - f_LastInterval);

            i_Frames = 0;

            f_LastInterval = Time.realtimeSinceStartup;
        }
    }
#endif


}
